package com.yicong.ants.utils.NoviceGuidance;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SizeUtils;
import com.yicong.ants.R;

import java.util.List;

public class GuidancePopup implements View.OnClickListener {
    private final PopupWindow popupWindow;
    private final View mView;
    private RelativeLayout groupView;
    private StepBean bean;
    private View.OnClickListener onClickListener;

    public GuidancePopup(Context context) {
        mView = LayoutInflater.from(context).inflate(R.layout.popup_guidance, null);
        groupView = mView.findViewById(R.id.group_layout);
        popupWindow = new PopupWindow(mView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setClippingEnabled(false);
        popupWindow.setContentView(mView);
    }

    public void setStep(StepBean b) {
        bean = b;
    }

    public void showDialog(View view) {
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        if (bean.status == 0){
            setView1(bean);
        }else if (bean.status == 1){
            setView2(bean);
        }
    }

    private void setView1(StepBean bean) {
        groupView.removeAllViews();
        int[] location = new int[2];
        bean.view.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];

        int w = SizeUtils.dp2px(10);
        RelativeLayout.LayoutParams layoutParams;

        ImageView imageBg = new ImageView(mView.getContext());
        imageBg.setId(R.id.guidance_image);
        imageBg.setBackgroundResource(R.drawable.shape_dashed_border);
        layoutParams = new RelativeLayout.LayoutParams(bean.view.getWidth() + w, bean.view.getHeight() + w);
        layoutParams.setMargins(x - w / 2, y - w / 2, 0, 0);
        groupView.addView(imageBg, layoutParams);

        w = SizeUtils.dp2px(6);

        ImageView imageView = new ImageView(mView.getContext());
        imageView.setBackgroundResource(R.drawable.shape_white_bg_12);
        imageView.setImageBitmap(createViewBitmap(bean.view));
        imageView.setAdjustViewBounds(true);
        imageView.setPadding(w, w, w, w);
        layoutParams = new RelativeLayout.LayoutParams(bean.view.getWidth() + w, bean.view.getHeight() + w);
        layoutParams.setMargins(x - w / 2, y - w / 2, 0, 0);
        groupView.addView(imageView, layoutParams);

        ImageView imageArrow = new ImageView(mView.getContext());
        imageArrow.setId(R.id.guidance_arrow);
        imageArrow.setImageResource(R.mipmap.img_guide_top);
        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.guidance_image);
        layoutParams.setMargins(SizeUtils.dp2px(10), y + SizeUtils.dp2px(50), 0, 0);
        groupView.addView(imageArrow, layoutParams);

        TextView textView = new TextView(mView.getContext());
        textView.setId(R.id.guidance_tips);
        textView.setTextSize(12);
        textView.setTextColor(ContextCompat.getColor(mView.getContext(), R.color.white));
        textView.setText("1.点击进行实名认证");
        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.BELOW, R.id.guidance_arrow);
        layoutParams.setMargins(SizeUtils.dp2px(90), SizeUtils.dp2px(10), 0, 0);
        groupView.addView(textView, layoutParams);

        TextView nextText = new TextView(mView.getContext());
        nextText.setOnClickListener(this);
        nextText.setPadding(SizeUtils.dp2px(15), SizeUtils.dp2px(6), SizeUtils.dp2px(15), SizeUtils.dp2px(6));
        nextText.setBackgroundResource(R.drawable.shape_white_line_25);
        nextText.setTextColor(ContextCompat.getColor(mView.getContext(), R.color.white));
        nextText.setText("下一步");
        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.BELOW, R.id.guidance_tips);
        layoutParams.setMargins(SizeUtils.dp2px(108), SizeUtils.dp2px(10), 0, 0);
        groupView.addView(nextText, layoutParams);
    }

    private void setView2(StepBean bean) {
        groupView.removeAllViews();
        int[] location = new int[2];
        bean.view.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];

        int w = SizeUtils.dp2px(10);
        RelativeLayout.LayoutParams layoutParams;

        ImageView imageBg = new ImageView(mView.getContext());
        imageBg.setId(R.id.guidance_image);
        imageBg.setBackgroundResource(R.drawable.shape_dashed_border);
        layoutParams = new RelativeLayout.LayoutParams(bean.view.getWidth() + w, bean.view.getHeight() + w);
        layoutParams.setMargins(x - w / 2, y - w / 2, 0, 0);
        groupView.addView(imageBg, layoutParams);

        w = SizeUtils.dp2px(6);

        ImageView imageView = new ImageView(mView.getContext());
        imageView.setBackgroundResource(R.drawable.shape_white_bg_12);
        imageView.setImageBitmap(createViewBitmap(bean.view));
        imageView.setAdjustViewBounds(true);
        imageView.setPadding(w, w, w, w);
        layoutParams = new RelativeLayout.LayoutParams(bean.view.getWidth() + w, bean.view.getHeight() + w);
        layoutParams.setMargins(x - w / 2, y - w / 2, 0, 0);
        groupView.addView(imageView, layoutParams);

        ImageView imageArrow = new ImageView(mView.getContext());
        imageArrow.setId(R.id.guidance_arrow);
        imageArrow.setImageResource(R.mipmap.img_guide_top);
        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.guidance_image);
        layoutParams.setMargins(SizeUtils.dp2px(10), y + SizeUtils.dp2px(50), 0, 0);
        groupView.addView(imageArrow, layoutParams);

        TextView textView = new TextView(mView.getContext());
        textView.setId(R.id.guidance_tips);
        textView.setTextSize(12);
        textView.setTextColor(ContextCompat.getColor(mView.getContext(), R.color.white));
        textView.setText("2.去任务中心领取推广任务");
        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.BELOW, R.id.guidance_arrow);
        layoutParams.setMargins(SizeUtils.dp2px(90), SizeUtils.dp2px(10), 0, 0);
        groupView.addView(textView, layoutParams);

        TextView nextText = new TextView(mView.getContext());
        nextText.setOnClickListener(this);
        nextText.setPadding(SizeUtils.dp2px(15), SizeUtils.dp2px(6), SizeUtils.dp2px(15), SizeUtils.dp2px(6));
        nextText.setBackgroundResource(R.drawable.shape_white_line_25);
        nextText.setTextColor(ContextCompat.getColor(mView.getContext(), R.color.white));
        nextText.setText("下一步");
        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.BELOW, R.id.guidance_tips);
        layoutParams.setMargins(SizeUtils.dp2px(108), SizeUtils.dp2px(10), 0, 0);
        groupView.addView(nextText, layoutParams);
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public void onClick(View view) {
        popupWindow.dismiss();
        if (onClickListener!=null){
            onClickListener.onClick(view);
        }
    }

    private Bitmap createViewBitmap(View v) {
        //创建一个和View大小一样的Bitmap
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        //使用上面的Bitmap创建canvas
        Canvas canvas = new Canvas(bitmap);
        //把View画到Bitmap上
        v.draw(canvas);
        return bitmap;
    }

    public static class StepBean {
        public View view;
        public int status;
    }
}
